package recursion;

import java.math.BigInteger;

public class FibonacciDemo {

    //普通算法
    public static long fact(long n) {
        if(n==1 || n==2){
            return 1;
        }
        return fact(n - 1) + fact(n - 2); //递归表达式
    }

    //优化算法

    public static long fact(long n,long pre,long next){
        if(n==2){
            return next;
        }
        return  fact(n-1,next,pre+next);
    }


    //改进为大整型实现
    public static BigInteger fact2(long n, BigInteger pre, BigInteger next){
        if(n==2){
            return next;
        }

        return  fact2(n-1,next,pre.add(next));
    }

    public static void main(String[] args) {

        long n = 100;
        BigInteger pre = new BigInteger("1");
        BigInteger next = new BigInteger("1");
        System.out.println("第" + n + "项是：" + fact2(n,pre,next));
    }
}
